package com.example.tliasfight.aop;

import com.alibaba.fastjson.JSONObject;
import com.example.tliasfight.mapper.OperateLogMapper;
import com.example.tliasfight.pojo.OperateLog;
import com.example.tliasfight.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class FightAsspect
{
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OperateLogMapper operateLogMapper;
    @Pointcut("@annotation(com.example.tliasfight.anno.Mylog)")
    public void pointCut(){}

    @Around("pointCut()")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {


//       if(request.getHeader("token")==null)
             log.info("原始方法的执行前逻辑");
        Long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();//这个地方，执行的是Controller的增删改方法，所以拿的也是Controller的增删改方法的结果，就是Result对象，然后我把Result的实例化对象给用字符串的形式给表示出来，并把这个字符串的形式赋值给ReturnValue这个日志对象的成员变量当中插入到相应的表格
        // String resultJsonStr=JSONObject.toJSONString(result);
        Long endTime = System.currentTimeMillis();
           log.info("原始方法的执行后逻辑");
        Long costTime=endTime-startTime;
                 //登录员工的id
           Claims claims =JwtUtils.parseJwt(request.getHeader("token"));
           Integer operatorUserid = (Integer)claims.get("id");



            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            Object[] args1 = joinPoint.getArgs();
            String args =Arrays.toString(args1);
            OperateLog operateLog = new OperateLog(null, operatorUserid, LocalDateTime.now(),className,methodName,args,result.toString(),costTime);
            operateLogMapper.insert(operateLog);
             log.info("插入日志对象成功");
            return result;

//        try {
//
//         //原始方法的调用
//         result = joinPoint.proceed();
//
//         Long endTime = System.currentTimeMillis();
//         Long costTime=endTime-startTime;
//
//            //登录员工的id
//           Claims claims =JwtUtils.parseJwt(request.getHeader("token"));
//           Integer id = (Integer)claims.get("id");
//
//
//
//            String className = joinPoint.getTarget().getClass().getName();
//            String methodName = joinPoint.getSignature().getName();
//            Object[] args1 = joinPoint.getArgs();
//            String args =Arrays.toString(args1);
//            OperateLog operateLog = new OperateLog(null,id, LocalDateTime.now(),className,methodName,args,(String)result,costTime);
//            operateLogMapper.insert(operateLog);
//            return result;
//            }
//       catch (Throwable e)
//             {  //登录员工的id
//           Claims claims =JwtUtils.parseJwt(request.getHeader("token"));
//           Integer id = (Integer)claims.get("id");
//
//                 Long endTime = System.currentTimeMillis();
//               Long costTime=endTime-startTime;
//
//            String className = joinPoint.getTarget().getClass().getName();
//            String methodName = joinPoint.getSignature().getName();
//            Object[] args1 = joinPoint.getArgs();
//            String args =Arrays.toString(args1);
//            OperateLog operateLog = new OperateLog(null,id, LocalDateTime.now(),className,methodName,args,(String)result,costTime);
//            operateLogMapper.insert(operateLog);
//            return result;
//             }
    }

}
